<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/5
 * Time: 18:19
 */
namespace Wap\Controller;
class CustController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->is_login ();
        $this->action = strtolower(ACTION_NAME);
        $this->title = '客源';
    }
    public function index (){
        $model = M ('customs');
        $model->table (array ('yk_customs'=>'c'));
        $map['c.state'] = array ('neq', 5);
        $data = I ('get.');
        if (!empty ($data['demand'])) $map['demand'] =  array ('eq', $data['demand']);
        if (!empty ($data['price'])){
            $price = explode('-', $data['price']);
            $map['_string'] = 'c.price like "%'.$price[0].'%" or c.price like "%'.$price[1].'%"';
        }
        if (!empty ($data['area'])){
            $area = explode('-', $data['area']);
            $map['_string'] = 'c.area like "%'.$area[0].'%" or c.area like "%'.$area[1].'%"';
        }
        if (!empty ($data['need_type'])){
            $need_type = explode('-', $data['need_type']);
            if (count ($need_type) == 2){
                $map['c.need_type'] = array ('like', '%'.$need_type[1].'%');
            }else{
                $map['_string'] = '(c.need_type not like "%0%") and
                 (c.need_type not like "%1%")
                  and (c.need_type not like "%2%") 
                  and (c.need_type not like "%3%") 
                  and (c.need_type not like "%4%")
                   and (c.need_type not like "%5%")';
            }
        }
       //var_dump($map);exit;
        if (IS_POST){
            $min = I ('post.price_min', 0);
            $max = I ('post.price_max', 100);
            $map['_string'] = 'c.price like %'.$min.'-'.$max.'%';
        }

        $model->field ('c.*,es.title as ename');
        $model->join ('yk_estate es on c.estate_id=es.id');
        $model->where ($map);
        $model->order ('c.add_time desc');
        $model->limit ('0,10');
        $customs = $model->select ();//echo $model->getLastSql();exit;

        $this->assign ('page', 1);
        $this->assign ('map', json_encode($map));
        $this->assign ('customs', $customs);//var_dump($customs);exit;
        $this->display ();
    }
    public function loading (){
        $json_map = I ('post.map', '');
        $ori_page = I ('post.page', '');
        $map = json_decode ($json_map);
        $page = $ori_page*10;
        $field = 'c.*,es.title as ename';
        $table = array ('yk_customs'=>'c');
        $customs = M ('customs')->field ($field)->table ($table)
            ->join ('yk_estate es on c.estate_id=es.id')
            ->where ($map)
            ->limit ($page,10)
            ->select ();

        if ($customs){
            foreach ($customs as &$c){
                $c['price'] = $c['price']==0? '不限':  $c['price'];
                $c['area'] = $c['area']==0? '不限':  $c['area'];
                $c['need_type'] = $c['need_type']==0? '不限':  $c['need_type'];
            }
            exit (json_encode(array ('state'=>'ok','data'=>$customs,'map'=>$json_map,'page'=>$ori_page+1)));
        }
        exit (json_encode(array ('state'=>'0','data'=>'','map'=>$json_map,'page'=>$ori_page+1)));
    }
    public function add (){
        if(IS_POST){
            $data = I ('post.');
            $data['add_time'] = date ('Y-m-d H:i:s');
            $data['user_id'] = $this->user['id'];
            $res = M ('customs')->add ($data);
            if ($res){
                $this->redirect (U('cust/save',array ('id'=>$res)));
            }else{
                $this->error ('信息有误！');
            }

        }

        $this->display ();
    }
    function save (){
        if (IS_POST){
            $data = I ('post.');
            $res = M ('customs')->save ($data);
            if ($res === false){
                $this->error ('录入失败');
            }else{
                $this->redirect(U('cust/index'));
            }
        }
        $cid = I ('get.id');
        $this->assign ('cid', $cid);
        $this->display ();
    }
}